/*
 * @Descripttion: 
 * @version: 
 * @Author: yuanliutech
 * @Date: 2022-11-22 10:08:14
 * @LastEditors: yuanliutech@lwj
 * @LastEditTime: 2023-01-04 19:28:13
 */
const app = getApp()
import {
  showMsg
} from '../../../../../utils/config.js'
import {
  reqSpaceList,
} from '../../../../../request/api'

module.exports = Behavior({
  data: {
    // 空间切换弹窗开始
    showSpace: false, //空间切换弹窗
    activeSpace: '',
    buildList: [],
    buildPage: 1,
    buildIsNextPage: true,
    buildId: '',
    floorList: [],
    floorPage: 1,
    floorIsNextPage: true,
    floorId: '',
    roomList: [],
    roomPage: 1,
    roomIsNextPage: true,
    roomId: '', //选中的安装点id
    spaceName: '', //展示的名称
    spaceId: '',
    // 空间切换弹窗结束
  },
  methods: {
    // 清除楼层安装点选中状态
    getClone() {
      let {
        buildList,
        floorList,
        roomList
      } = this.data
      floorList.forEach(item => {
        item.active = false
      })
      roomList.forEach(item => {
        item.active = false
      })

      this.setData({
        buildList,
        floorList: [],
        roomList: [],
        // activeSpace: 'build',
        activeSpace: '',
        floorId: '',
        roomId: '',
      })
    },
    // 建筑物回到最初状态
    getInitBuildList() {
      this.setData({
        buildPage: 1,
        buildIsNextPage: true,
        buildList: []
      })
      this.getBuildList()
    },
    // 点击安装点
    clickShowSpace() {
      if (!this.data.form.orgId) return showMsg("请先选择单位")
      this.setData({
        showSpace: true
      })
    },
    /**
     * @name: 获取建筑物列表
     * @msg: *parkId：园区id  *pid：父id  name：空间名称
     * @return {*}
     */
    async getBuildList() {
      let params = {
        orgId: this.data.form.orgId,
        pid: '0',
        name: '',
        userId: app.globalData.userId,
        roleName: app.globalData.rolename,
        tenantId: app.globalData.tenantId,
      }
      const res = await reqSpaceList(this.data.buildPage, 20, params)
      if (res.code !== 20000) return showMsg(res.message)
      let {
        rows
      } = res.data
      if (this.data.buildPage === 1) {
        this.setData({
          buildList: rows,
          buildIsNextPage: true
        })
      } else if (this.data.buildPage > 1 && rows.length) {
        this.setData({
          buildList: this.data.buildList.concat(rows),
          buildIsNextPage: true
        })
      } else if (this.data.buildPage > 1 && !rows.length) {
        this.setData({
          buildIsNextPage: false
        })
      }
    },
    // 滚动到底部触发
    buildLower() {
      if (!this.data.buildIsNextPage) return
      this.setData({
        buildPage: this.data.buildPage + 1,
      })
      this.getBuildList()
    },
    // 点击建筑物
    clickBuild(e) {
      console.log(e.currentTarget.dataset, '点击建筑物');
      let {
        id
      } = e.currentTarget.dataset
      let {
        buildList,
        floorList,
        roomList
      } = this.data
      buildList.forEach(item => {
        if (item.id === id) {
          item.active = true
          this.getInitFloorList(item.id)
        } else {
          item.active = false
        }
      })
      floorList.forEach(item => {
        item.active = false
      })
      roomList.forEach(item => {
        item.active = false
      })
      this.setData({
        buildList,
        floorList,
        roomList: [],
        activeSpace: 'build',
        buildId: id,
        floorId: '', //楼层id清空
        roomId: '', //安装点id清空
      })
    },
    /**
     * @name: 获取楼层列表
     * @msg: *parkId：园区id  *pid：父id  name：空间名称
     * @return {*}
     */
    async getFloorList(pid) {
      let params = {
        orgId: this.data.form.orgId,
        pid,
        name: '',
        userId: app.globalData.userId,
        roleName: app.globalData.rolename,
        tenantId: app.globalData.tenantId,
      }
      const res = await reqSpaceList(this.data.floorPage, 20, params)
      if (res.code !== 20000) return showMsg(res.message)
      let {
        rows
      } = res.data
      if (this.data.floorPage === 1) {
        this.setData({
          floorList: rows,
          floorIsNextPage: true
        })
      } else if (this.data.floorPage > 1 && rows.length) {
        this.setData({
          floorList: this.data.floorList.concat(rows),
          floorIsNextPage: true
        })
      } else if (this.data.floorPage > 1 && !rows.length) {
        this.setData({
          floorIsNextPage: false
        })
      }
    },
    // 滚动到底部触发
    floorLower() {
      if (!this.data.floorIsNextPage) return
      this.setData({
        floorPage: this.data.floorPage + 1,
      })
      this.getFloorList(this.data.buildId)
    },
    // 楼层回到初始状态
    getInitFloorList(id) {
      this.setData({
        floorPage: 1,
        floorIsNextPage: true,
        floorList: []
      })
      this.getFloorList(id)
    },
    // 点击楼层
    clickFloor(e) {
      console.log(e.currentTarget.dataset);
      let {
        id
      } = e.currentTarget.dataset
      let {
        floorList,
        roomList,
      } = this.data
      floorList.forEach(item => {
        if (item.id === id) {
          item.active = true
          this.getInitRoomList(item.id)
        } else {
          item.active = false
        }
      })
      roomList.forEach(item => {
        item.active = false
      })
      this.setData({
        floorList,
        roomList,
        activeSpace: 'floor',
        floorId: id,
        roomId: '', //安装点id清空
      })

    },
    /**
     * @name: 获取房间列表
     * @msg: *parkId：园区id  *pid：父id  name：空间名称
     * @return {*}
     */
    async getRoomList(pid) {
      let params = {
        orgId: this.data.form.orgId,
        pid,
        name: '',
        userId: app.globalData.userId,
        roleName: app.globalData.rolename,
        tenantId: app.globalData.tenantId,
      }
      const res = await reqSpaceList(this.data.roomPage, 20, params)
      if (res.code !== 20000) return showMsg(res.message)
      let {
        rows
      } = res.data
      if (this.data.roomPage === 1) {
        this.setData({
          roomList: rows,
          roomIsNextPage: true
        })
      } else if (this.data.roomPage > 1 && rows.length) {
        this.setData({
          roomList: this.data.roomList.concat(rows),
          roomIsNextPage: true
        })
      } else if (this.data.roomPage > 1 && !rows.length) {
        this.setData({
          roomIsNextPage: false
        })
      }
    },
    // 滚动到底部触发
    roomLower() {
      if (!this.data.roomIsNextPage) return
      this.setData({
        roomPage: this.data.roomPage + 1,
      })
      this.getRoomList(this.data.floorId)
    },
    // 房间回到初始状态
    getInitRoomList(id) {
      this.setData({
        roomPage: 1,
        roomIsNextPage: true,
        roomList: []
      })
      this.getRoomList(id)
    },
    // 点击房间
    clickRoom(e) {
      console.log(e.currentTarget.dataset);
      let {
        id
      } = e.currentTarget.dataset
      let {
        roomList
      } = this.data
      roomList.forEach(item => {
        if (item.id === id) {
          item.active = true
        } else {
          item.active = false
        }
      })
      this.setData({
        roomList,
        activeSpace: 'room',
        roomId: id
      })
    },
    // 点击关闭空间切换弹窗
    closeSpace() {
      this.setData({
        showSpace: false
      })
    },
    // 点击确认选择的空间
    confirmSpace() {
      let {
        activeSpace,
        buildList,
        buildId,
        floorList,
        floorId,
        roomList,
        roomId,
        spaceName
      } = this.data
      // if (activeSpace !== 'room') return showMsg("设备需要安装在安装点下，请选择安装点")
      spaceName = ''
      buildList.forEach(item => {
        if (buildId === item.id) {
          spaceName += item.name + '-'
        }
      })
      floorList.forEach(item => {
        if (floorId === item.id) {
          spaceName += item.name + '-'
        }
      })
      roomList.forEach(item => {
        if (roomId === item.id) {
          spaceName += item.name
        }
      })
      let spaceId = activeSpace === 'room' ? roomId : activeSpace === 'floor' ? floorId : buildId //
      this.setData({
        showSpace: false,
        spaceName,
        spaceId: spaceId
      })
      if (this.data.roomId == spaceId) {
        // 选的是安装点
        this.AlarmReceiverBySpaceId() // 空间id获取接警人列表
      } else {
        this.setData({
          alarmReceiverIdList1: [],
          alarmReceiverIdList2: [],
          alarmReceiverIdList3: [],
          alarmReceiverIdList4: [],
        })
      }
      // if(roomId!=''){
      //     this.setData({
      //         showSpace: false,
      //         spaceName,
      //         spaceId: roomId//安装点id
      //     })
      // }else if(floorId!=''){
      //     this.setData({
      //         showSpace: false,
      //         spaceName,
      //         spaceId: floorId//楼层id
      //     })
      // }else{
      //     this.setData({
      //         showSpace: false,
      //         spaceName,
      //         spaceId: buildId//建筑物id
      //     })
      // }

    },
    // 点击确认 -- 接警人详情使用
    confirmAlarmReceiverDetailSpace() {
      let {
        activeSpace,
        buildId,
        floorId,
        roomId,
        roomList
      } = this.data
      // if(activeSpace=='floor' && roomList.length==0) return showMsg('无安装点不能进行关联');
      if (activeSpace == '') return showMsg('无安装点不能进行关联');
      let spaceId = activeSpace === 'room' ? roomId : activeSpace === 'floor' ? floorId : buildId
      this.setData({
        showSpace: false,
      })
      this.spaceReceiverAddFun(spaceId)
    },

    // 去新增空间
    goAddSpace() {
      let {
        parkId,
        activeSpace,
        buildId,
        floorId,
      } = this.data
      console.log(activeSpace);
      console.log(this.data.form.orgId);
      if (!activeSpace) {
        wx.navigateTo({
          url: `/packageA/pages/index-inside-page/building-add/building-add?orgId=${this.data.form.orgId}`,
        })
      } else if (activeSpace === 'build') {
        let info = {
          pid: buildId,
          orgId: this.data.form.orgId,
          parkId,
        }
        let params = JSON.stringify(info)
        wx.navigateTo({
          url: `/packageA/pages/index-inside-page/floor-add/floor-add?params=${params}`,
        })
      } else if (activeSpace === 'floor') {
        let info = {
          pid: floorId,
          orgId: this.data.form.orgId,
          parkId,
        }
        let params = JSON.stringify(info)
        wx.navigateTo({
          url: `/packageA/pages/index-inside-page/room-add/room-add?params=${params}`,
        })
      }
    },
    // 加载space数据
    getSpaceList() {
      let {
        activeSpace,
        buildId,
        floorId
      } = this.data
      if (!activeSpace) {
        this.getBuildList() // 获取建筑物列表
      } else if (activeSpace === 'build') {
        this.getFloorList(buildId)
      } else if (activeSpace === 'floor') {
        this.getRoomList(floorId)
      }
    },
  }
})